<?php # 订单

namespace app\controllers;

use yii;
use yii\Web\Controller;
use app\components\BaseController;
use app\components\WechatJsApiPay;
use app\models\Orders;
use app\models\User;

class OrdersController extends BaseController
{
    const APPID = 'wx426b3015555a46be';
    const MCHID = '1225312702'; // 商户号
    const KEY = 'e10adc3949ba59abbe56e057f20f883e';
    const APPSECRET = '01c6d59a3f9024db6336662ac95c8e74';


    public $layout = 'main';
    public $enableCsrfValidation = false;

    /**
    * 购买VIP会员
    */
    public function actionIndex()
    {
        $userinfo = $this->getCurrentUser();
        // $userinfo = array('openid' => 'o_HQ5t6Ww1EW8qbRmWF2lJUHWhFE', 'uid' => 1, 'nickname' => 'ta_shuo', 'gender' => 1, 'province' => '福建', 'city' => '厦门', 'headimgurl' => 'http://wx.qlogo.cn/mmopen/rVQIW9nZwdNNibkm1beQlt7eMF45qib7ibsG8dWcgxe7ibnykLOfJMwgmvTvv3oic7plNfdoxOI3RckCYzJRbwheIkvfJZZheEMnc/0');

        return $this->render('index', ['userinfo' => $userinfo, 'title' => '购买VIP会员']);
    }

    /**
    * 发起订单
    */
    public function actionCreate()
    {
        $userinfo = $this->getCurrentUser(false);
        // $userinfo = array('openid' => 'o_HQ5t6Ww1EW8qbRmWF2lJUHWhFE', 'uid' => 1, 'nickname' => 'ta_shuo', 'gender' => 1, 'province' => '福建', 'city' => '厦门', 'headimgurl' => 'http://wx.qlogo.cn/mmopen/rVQIW9nZwdNNibkm1beQlt7eMF45qib7ibsG8dWcgxe7ibnykLOfJMwgmvTvv3oic7plNfdoxOI3RckCYzJRbwheIkvfJZZheEMnc/0');

        if(!empty($userinfo) && !empty($_POST)) {
        	// 如果不存在该用户则插入user表
	if(!isset($userinfo['uid'])) {
	    $fans = new Fans();
	    $fans->openid = $userinfo['openid'];
	    $fans->score = 0;

	    if($fans->save()) {
	        $userinfo['uid'] = Yii::$app->db->getLastInsertID();
	        $this->setCurrentUser($userinfo);
	    }else {

	    }
	}

	$current_time = time();
        	$orders = new Orders();
        	$orders->uid = $userinfo['uid'];
        	$orders->username = $_POST['username'];
        	$orders->phone = $_POST['phone'];
        	$orders->type = 1;
        	$orders->count = $_POST['count'];
        	$orders->create_at = $current_time;
        	$orders->pay_at = 0;

        	if($orders->save()) {
        	    $order_id = Yii::$app->db->getLastInsertID();
        	    var_dump($order_id);exit;
        	    Yii::$app->session->setFlash('pay_orderid', $order_id);
        	    Yii::$app->session->setFlash('pay_money', $_POST['count'] * 10);
        	    echo $order_id;
        	} else {
        	    echo 0;
        	}
        }
    }

    // 个人订单列表
    public function actionList()
    {
        $userinfo = $this->getCurrentUser();
        // $userinfo = array('openid' => 'o_HQ5t6Ww1EW8qbRmWF2lJUHWhFE', 'uid' => 1, 'nickname' => 'ta_shuo', 'gender' => 1, 'province' => '福建', 'city' => '厦门', 'headimgurl' => 'http://wx.qlogo.cn/mmopen/rVQIW9nZwdNNibkm1beQlt7eMF45qib7ibsG8dWcgxe7ibnykLOfJMwgmvTvv3oic7plNfdoxOI3RckCYzJRbwheIkvfJZZheEMnc/0');

        $order_list = Orders::find()
        			->where(['uid' => $userinfo['uid']])
        			->all();

        return $this->render('list', ['userinfo' => $userinfo, 'order_list' => $order_list, 'title' => '我的订单列表']);
    }

    // 支付页面
    public function actionPay()
    {
        // var_dump(Yii::$app->session->getFlash('pay_orderid'));exit;
        $userinfo = $this->getCurrentUser();
        // $userinfo = array('openid' => 'o_HQ5t6Ww1EW8qbRmWF2lJUHWhFE', 'uid' => 1, 'nickname' => 'ta_shuo', 'gender' => 1, 'province' => '福建', 'city' => '厦门', 'headimgurl' => 'http://wx.qlogo.cn/mmopen/rVQIW9nZwdNNibkm1beQlt7eMF45qib7ibsG8dWcgxe7ibnykLOfJMwgmvTvv3oic7plNfdoxOI3RckCYzJRbwheIkvfJZZheEMnc/0');

        // 用户订单信息
        $user_order = Orders::findOne($_GET['order_id']);
        if(empty($user_order)) {
        	// exit('订单不存在');
        }

        if($user_order['uid'] != $userinfo['uid']) {
        	// exit('非法订单');
        }

        if(!empty($user_order['pay_at'])) {
        	// exit('已支付');
        }

        $money = $user_order['count'] * 10;

        // 获取订单信息
        $wx_order = array();
        $wx_order['appid'] = self::APPID;
        $wx_order['body'] = 'vip会员购买';
        $wx_order['attach'] = '购买';
        $wx_order['mch_id'] = self::MCHID;
        $wx_order['nonce_str'] = WechatJsApiPay::getNonceStr(16);
        $wx_order['openid'] = $userinfo['openid'];
        $wx_order['out_trade_no'] = self::MCHID.date('YmdHis', time());
        $wx_order['total_fee'] = 1;
        $wx_order['trade_type'] = 'JSAPI';
        $wx_order['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];



        return $this->render('pay', ['userinfo' => $userinfo, 'title' => '支付页面']);
    }
}